﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WAV
{
    public abstract class CodecBase
    {
        protected const Int32 DataString = 0x61746164;      // "data"
        protected const Int32 FormatString = 0x20746D66;    // "fmt "
		protected const Int32 SmplString = 0x6C706D73;      // "smpl"
		protected const Int32 WaveString = 0x45564157;      // "WAVE"

        protected byte[] Data;
        protected CodecProperties Properties;

        // set 8-bit data
        public void SetData(byte[] newData)
        {
            //Properties.BitsPerSample = 8;
            SetDataInternal(newData);
        }

        // set 16-bit data
        public void SetData(short[] newData)
        {
            MemoryStream buffer = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(buffer);
            int length = newData.Length;

            Properties.BitsPerSample = 16;

            // convert short data to bytes
            for (int i = 0; i < length; i++)
            {
                writer.Write(newData[i]);
            }

            SetDataInternal(buffer.ToArray());
            writer.Close();
            buffer.Dispose();
        }

        // internal set data function
        protected void SetDataInternal(byte[] newData)
        {
            Data = new byte[newData.Length];
            Array.Copy(newData, Data, Data.Length);
        }
    }
}
